/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.imagelettericon;

import com.github.akashohos90.imageletter.drawable.TextDrawable;
import com.imagelettericon.util.TextUtils;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.render.Canvas;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;

/**
 * Contacts Fragment
 *
 * @since 2021-05-27
 */
public class ContactsFragment extends BaseFraction {
    private TextDrawable.IBuilder mTextDrawableBuilder;

    @Override
    public int getUiContent() {
        return ResourceTable.Layout_fragment_contacts;
    }

    @Override
    public void initComponent() {
        mTextDrawableBuilder = TextDrawable.builder().beginConfig()
                .fontSize(VALUE_FONT_SIZE)
                .endConfig()
                .rect();
        ListContainer list = (ListContainer) mComponent.findComponentById(ResourceTable.Id_lc);
        list.setItemProvider(new SampleItemProvider());
    }

    /**
     * SampleItemProvider
     *
     * @since 2021-05-27
     */
    private class SampleItemProvider extends BaseItemProvider {
        @Override
        public int getCount() {
            return REMARKS_CONTACTS.length;
        }

        @Override
        public String getItem(int position) {
            return REMARKS_CONTACTS[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public Component getComponent(int position, Component convertView, ComponentContainer componentContainer) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_layout_list_item,
                        null, false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            String item = getItem(position);
            updateCheckedState(holder, item);
            holder.getTextView().setText(item);
            return convertView;
        }

        private void updateCheckedState(ViewHolder holder, String item) {
            int color = Color.getIntColor(MATERIAL_COLORS[mSecureRandom.nextInt(MATERIAL_COLORS.length)]);
            TextDrawable drawable =
                    mTextDrawableBuilder.build(TextUtils.computeText(item, VALUE_TWO), color);
            holder.getImageView().addDrawTask(new Component.DrawTask() {
                @Override
                public void onDraw(Component component, Canvas canvas) {
                    drawable.setBounds(new Rect(0, 0,
                            holder.getImageView().getWidth(), holder.getImageView().getHeight()));
                    drawable.drawToCanvas(canvas);
                }
            });
        }
    }
}